package com.zx.notification.producer.controller.template;

import com.zx.core.wrapper.ObjectWrapper;
import com.zx.feign.client.dto.GetNotificationTemplateResponseDTO;
import com.zx.notification.common.model.NotificationTemplates;
import com.zx.notification.producer.service.INotificationTemplatesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * description: 通知模板控制器 <br>
 * create: 2025-10-23
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/notification-templates")
@RequiredArgsConstructor
public class NotificationTemplatesController {

    private final INotificationTemplatesService iNotificationTemplatesService;

    /**
     * 根据模板编码获取通知模板
     *
     * @param templateCode 模板编码
     * @return 通知模板信息
     */
    @GetMapping("/getByCode/{templateCode}")
    public ObjectWrapper<GetNotificationTemplateResponseDTO> getTemplateByCode(@PathVariable("templateCode") String templateCode) {
        ObjectWrapper<GetNotificationTemplateResponseDTO> result = new ObjectWrapper<>();
        try {
            NotificationTemplates template = iNotificationTemplatesService.getTemplateByCode(templateCode);
            if (template != null) {
                GetNotificationTemplateResponseDTO dto = new GetNotificationTemplateResponseDTO(
                        template.getId(),
                        template.getTemplateCode(),
                        template.getTemplateName(),
                        template.getBizType(),
                        template.getTemplateContent(),
                        template.getSupportedChannels(),
                        template.getConsumeMode(),
                        template.getStatus(),
                        template.getCreateBy(),
                        template.getCreateTime(),
                        template.getUpdateBy(),
                        template.getUpdateTime(),
                        template.getRemark()
                );
                result.setData(dto);
            } else {
                result.fail("模板不存在");
            }
        } catch (Exception e) {
            result.fail("获取模板信息失败: " + e.getMessage());
        }
        return result;
    }
}